﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlackOff
{
    public class TimeStateContext : ITimeStateContext
    {

        private readonly SlackOffOptions options;
        private readonly System.Windows.Forms.Timer timer;

        public event EventHandler Tick = default!;

        private ITimeState state;

        public TimeStateContext(SlackOffOptions options, System.Windows.Forms.Timer timer)
        {
            this.options = options;
            this.timer = timer;
            this.timer.Interval = 1000;
            this.timer.Enabled = true;
            this.timer.Tick += this.Timer_Tick;
            this.state = new NoneState(this);
        }

        public ITimeState State => this.state;

        public SlackOffOptions Options => options;

        private void Timer_Tick(object? sender, EventArgs e)
        {
            this.state.TryFinish();

            this.Tick.Invoke(this, EventArgs.Empty);
        }

        public void ChangeState(ITimeState state)
        {
            this.state = state;
        }

        public void Reset()
        {
            this.state = new NoneState(this);
        }
    }
}
